import mycuia from '@/utils/dev/blood/mycuia/mycuia.js'
import sinocare from '@/utils/dev/blood/sinocare/sinocare.js'
import gochek from '@/utils/dev/blood/gochek/gochek.js'
import blue from '@/utils/dev/blood/common/blue.js'
 

const conn =async (opt)=>{
	opt = opt || {};
	opt.data = opt.data || '';
	opt.bind =  opt.bind || function () {};
	opt.success =  opt.success || function () {};
	opt.fail =  opt.fail || function () {};
	opt.onBLEConnectionStateChange = opt.onBLEConnectionStateChange || function () {};
	opt.returnData = {userInfo:opt.data.userInfo}
	//初始蓝牙
	blue.openBluetoothAdapter().then(async sign=>{
		if(!sign)
		{
			return
		}
		const resp = await new Promise((resolve, reject) => {
		  blue.checkBind(opt.data.userInfo,opt.data.dev.deviceId,opt.bind).then(val=>{resolve(val)})
		})
		console.log('bloodjs fsdfsdf',resp)
		console.log('bloodjs factory',opt.data.factory)
		if (resp.status) {
			//设备绑定成功  连接设备
			let blood = ''
			switch(opt.data.factory) {
			     case 'mycuia':
			        // 民康
					blood = mycuia; 
			        break;
				  case 'sinocare':	 
					blood = sinocare; 
					break;
					case 'gochek':
					console.log('bloodjs','1111')
					blood = gochek; 
					break;
			}
			//判断是不是sdk连接蓝牙
			if(!opt.data.isSdkConn){
				blue.createBLEConnection(opt.data.dev,opt.success,opt.fail,opt.onBLEConnectionStateChange,blood)	
			}else{
				blue.sdkCreateBLEConnection(opt.data.dev,opt.success,opt.fail,opt.onBLEConnectionStateChange,blood)
			}
			 
			//this.stopBluetoothDevicesDiscovery()//此时停止蓝牙搜索

		} else {
		//设备绑定成功
			if (typeof resp.data.bindStatus != "undefined" && resp.data.bindStatus) {
				let blood = ''
				switch(opt.data.factory) {
				     case 'mycuia':
				        // 民康
						blood = mycuia;
				        break;
					  case 'sinocare': 
						blood = sinocare; 
						break;
						case 'gochek':
						console.log('bloodjs','22222')
						blood = gochek; 
						break;
				}
				
				if(!opt.data.isSdkConn){
					blue.createBLEConnection(opt.data.dev,opt.success,opt.fail,opt.onBLEConnectionStateChange,blood)	
				}else{
					blue.sdkCreateBLEConnection(opt.data.dev,opt.success,opt.fail,opt.onBLEConnectionStateChange,blood)
				}	
			} else {
				//设备已绑定失败
				// console.log(resp.code)
				opt.returnData.userInfo.deviceId = opt.data.dev.deviceId
				if(resp.code==2000)
				{
					opt.fail({status:0,code:7,data:opt.returnData,message:'设备已被其它用户绑定,请先解绑设备'});
					return
				}
				opt.fail({status:0,code:9,data:opt.returnData,message:'服务器繁忙,请重新连接设备'});
				return
			}
		}
	})


}

const closeBluetoothAdapter = ()=>{
	uni.closeBluetoothAdapter();
}

const getDevList = (opt)=>{
	opt = opt || {};
	opt.success =  opt.success || function () {};
	//初始蓝牙
	blue.openBluetoothAdapter().then(async sign=>{
		if(!sign)
		{
			return
		}
		uni.startBluetoothDevicesDiscovery({
				allowDuplicatesKey: true, //允许重复上报同一个设备
				success: (res) => {
					console.log("开始搜寻蓝牙设备成功(startBluetoothDevicesDiscovery success)", res);
					uni.showLoading({
						title: "正在搜索设备",
					});
					uni.onBluetoothDeviceFound((res) => {
						if (res) {
							uni.hideLoading();
						}
						res.devices.forEach(device => {
							//过滤掉没有名字的设备
							// console.log(device)
							if (!device.name && !device.localName) {
								return
							};
							opt.success(device)
							//这么操作是为了去除重复
						// 	const foundDevices = this.devices //将数据中的数组复制一份,利用动态绑定方式，不断复制最新的数组
						// 	const idx = this.inArray(foundDevices, 'deviceId', device.deviceId)


						// 	if (idx === -1 ) {
						// 		this.devices.push(device); //数组里没有的的就向里面添加数据，保证没有重复[uni写法]
						// 		// console.log('搜索到的设备组',device.name+'---'+device.deviceId+'*****'+JSON.stringify(device));
						// 	}

						})

					});
				},
			});
	});
} 

export default {
	conn,
	closeBluetoothAdapter,
	getDevList
}


